import { createApp } from "vue";
import "element-plus/dist/index.css";
import "./Style.scss";
import { createPinia } from "pinia";
import App from "./App.vue";
import router from "./router";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import piniaPersist from "pinia-plugin-persistedstate";
import UseuserPermission from "./stores/UserPermission";
const app = createApp(App);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const pinia = createPinia();
pinia.use(piniaPersist);
app.use(pinia);
// 路由刷新后的数据
const usePermission = UseuserPermission();
const UserPermission = localStorage.getItem("UserPermission");
console.log(UserPermission);
if (UserPermission) {
  console.log(JSON.parse(UserPermission));
  usePermission.dynamicMenu(JSON.parse(UserPermission).routerList);
  usePermission.routerList.forEach((item) => {
    console.log(item);
    router.addRoute("main", item);
  });
}
router.beforeEach((to, form, next) => {
  const token = localStorage.getItem("pz_token");
  if (!token && to.path !== "/login") {
    next("/login");
  } else if (token && to.path == "/login") {
    next("/");
  } else {
    return next();
  }
});
app.use(router);
app.mount("#app");
